package com.fenxiangtech.mall.manager;

import com.fenxiangtech.mall.dao.ProductDAO;
import com.fenxiangtech.mall.model.Product;
import com.fenxiangtech.mall.model.qc.Page;
import com.fenxiangtech.mall.model.qc.ProductQC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ProductManager {

    @Autowired
    private ProductDAO productDAO;

    public List<Product> queryProduct(Product product, Integer pageIndex , Integer pageSize){
        Page page = new Page(pageIndex,pageSize);
        ProductQC qc  = new ProductQC();
        qc.setPage(page);
        BeanUtils.copyProperties(product,qc);
        //属性copy
        List<Product> products = productDAO.listProduct(qc);
        return products;
    }

    public Long countProduct(Product product){
        ProductQC qc  = new ProductQC();
        //属性copy
        BeanUtils.copyProperties(product,qc);
        Long count = productDAO.countProduct(qc);
        return count;
    }

    public int saveProduct(Product product){

        if(product.getId()!=null){
            return productDAO.updateProduct(product);
        }else {
            product.setCreateTime(new Date());
            return productDAO.insertProduct(product);
        }

    }
    public int deleteProduct(Long id){

        return productDAO.deleteProductById(id);

    }



}